package com.twu.biblioteca;

import com.sun.xml.internal.fastinfoset.util.StringArray;

import java.io.*;
import java.util.*;

public class BibliotecaApp {

    private PrintStream out;

    public BibliotecaApp(PrintStream out) {
        this.out = out;
    }

    public static void main(String[] args) throws IOException {
        Book[] catalog;
        BibliotecaApp bibliotecaApp = new BibliotecaApp(System.out);
        catalog = bibliotecaApp.setCatalog();
        bibliotecaApp.menuManager(bibliotecaApp, catalog);
    }

    public Book[] setCatalog() {
        Book[] catalog = new Book[4];
        catalog[0] = new Book("The Lord of the Rings", true);
        catalog[1] = new Book("The Shadow of the Wind", false);
        catalog[2] = new Book("The Little Prince", true);
        catalog[3] = new Book("Simon's Cat", false);
        return catalog;
    }


    public void welcomeMessage() {
        String message = "Welcome to Biblioteca!";
        out.println(message);
    }

    public void menuOptions() {
        String message = "\n=== MENU OPTIONS === \n" +
                " 1 - Book Catalog\n" +
                " 2 - Reserve a Book\n" +
                " 3 - Check membership details\n" +
                " 4 - Exit \n";
        out.println(message);
    }
    public int getCustomerInput(String prompt, BufferedReader reader) throws IOException {
        String inputLine = null;
        out.println(prompt + "  ");
        inputLine = reader.readLine();
        if (inputLine == null || inputLine.length() == 0) return 0;
        return Integer.parseInt(inputLine);
    }


    public int getSelectedMenuOption() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int selectedOption = getCustomerInput("\nPlease, select an option.", reader);
        return selectedOption;
    }

    public void menuManager(BibliotecaApp bibliotecaApp, Book[] catalog) throws IOException {
        bibliotecaApp.welcomeMessage();
        int selectedMenuOption = 0;
        while (selectedMenuOption != 4)  {
            bibliotecaApp.menuOptions();
            selectedMenuOption = bibliotecaApp.getSelectedMenuOption();
            switch (selectedMenuOption) {
                case 1:
                    showBookCatalog();
                    break;
                case 2:
                    int selectedBookNumber = getBookReservation();
                    reserveBook(catalog, selectedBookNumber);
                    break;
                case 3:
                    out.println("Please talk to a Librarian. Thank you.");
                    break;
                default:
                    out.println("Select a valid option!!\n");
                    break;
            }
        }
        out.println("Good Bye!\n");
    }

    public void showBookCatalog() {
        out.println("*** Book Catalog ***\n" +
                "0. Tolkien, J.R.R. - The Lord of the Rings\n" +
                "1. Zafon, Carlos Ruiz - The Shadow of the Wind\n" +
                "2. Saint-Exupery, Antoine de - The Little Prince\n" +
                "3. Tofield, Simon - Simon's Cat\n" +
                "--- end of the catalog ---\n");
    }

    public int getBookReservation() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        return getCustomerInput("\nPlease, write the book number.", reader);
    }

    public void reserveBook(Book[] catalog, int selectedBookNumber) throws IOException {
        boolean isAvailable = checkIfAvailable(catalog, selectedBookNumber);

        if (isAvailable)  {
            catalog[selectedBookNumber].setAvailable(false);
            out.println("Thank You! Enjoy the book " + catalog[selectedBookNumber].getTitle() + "\n");
        } else
            out.println("Sorry we don't have that book yet.");
    }


    public boolean checkIfAvailable(Book[] catalog, int bookNumber) {
        return catalog[bookNumber].getAvailable();
    }
}
